44

Beginner’s Guide to Code Algorithms

44

STEP 7 continued

      Else

        home =​ i

      End If

    Next i

    If notahomecount =​ 8 Then

        sbox(home, j) =​ putnumber

        Call updatecantbelist(putnumber, home, j)

        Cells(home, j) =​ sbox(home, j)

      End If

    End If

Next j

:

:

Next putnumber

STEP 8

Now check all cells in the 3 by 3 grid for all numbers 1–​9 (putnumber)

if the number is invalid in eight of the nine cells in the same row, then the number

must belong to the ninth cell (empty, of course).

For putnumber =​ 1 to 9

:

:

For i =​ 1 To 3

For j =​ 1 To 3

    For k =​ 1 To 3

      n =​ Int((i -​ 1) /​ 3) * 3 +​ k

    For l =​ 1 To 3

      p =​ Int((j -​ 1) /​ 3) * 3 +​ l

      numberfound =​ 0

      If sbox(n, p) =​ putnumber Then numberfound =​ 1

    Next l

Next k

If numberfound =​ 0 Then

    notahomecount =​ 0

    For k =​ 1 To 3

      n =​ Int((i -​ 1) /​ 3) * 3 +​ k

      For l =​ 1 To 3

        p =​ Int((j -​ 1) /​ 3) * 3 +​ l

        notahome =​ 0

        If sbox(n, p) <> ““ Then notahome =​ 1

        For kk =​ 1 To 9

          If cantbelist(n, p, kk) =​ putnumber Then notahome =​ 1

        Next kk

        If notahome =​ 1 Then

          notahomecount =​ notahomecount +​ 1

        Else

          homerow =​ n

          homecol =​ p

        End If